package com.xxxx;

import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

import java.util.Arrays;
import java.util.Random;

public class Hello05FlinkOperatorKeyby {

    public static void main(String[] args) throws Exception {

        //获取执行环境
        StreamExecutionEnvironment environment = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
        //获取数据源
        DataStreamSource<String> source = environment.fromElements("a1 a2 a3 a4 a5 a6", "a1 a2 a3 aa4 aa5 aa6");
        //开始进行转换
        SingleOutputStreamOperator<Tuple3<String, String, Integer>> dataStream = source.flatMap((String line, Collector<Tuple3<String, String, Integer>> collector) -> {
            Arrays.stream(line.split(" ")).forEach(word -> collector.collect(Tuple3.of(word, "hello", new Random().nextInt(1000))));
        }).returns(Types.TUPLE(Types.STRING, Types.STRING, Types.INT));
        //开始进行分组(0，第一个元素)
        //dataStream.keyBy(0).sum(2).print();
        //开始进行分组(1，第二个元素，因为都是Hello)
        //dataStream.keyBy(1).sum(2).print();
        //开始进行分组(0,1，根据第一和第二个元素进行分组)
        //dataStream.keyBy(0, 1).sum(2).print();
        dataStream.print();
        dataStream.keyBy(1).minBy(2).print();

        //开始执行
        environment.execute("Hello05FlinkOperatorKeyby" + System.currentTimeMillis());
    }


}
